package com.guanyu.u2cache.service;

import com.guanyu.u2cache.annotation.U2CacheEvict;
import com.guanyu.u2cache.annotation.U2CachePut;
import com.guanyu.u2cache.annotation.U2Cacheable;
import com.guanyu.u2cache.cache.EventPush;
import com.guanyu.u2cache.domain.Teacher;
import com.guanyu.u2cache.enums.CacheType;
import com.guanyu.u2cache.enums.EventType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author zhouyunjian
 * @date 2023/11/02 13:42
 **/
@Slf4j
@Component
public class CacheTestService {

    @U2Cacheable(cacheNames = {"localCache"}, key = "#root.methodName", cacheType = CacheType.LEVEL1CACHE, expireTime = 50, expireUnit = TimeUnit.SECONDS, continueLife = true, preHeat = true)
    public Teacher findAllInfo() {
        log.info("进入缓存方法");
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
//        Map<String, String> map = new HashMap<>(100);
//        map.put("name", "zhou");
//        map.put("address", "hangzhou");
        return teacher;
    }

    @U2CacheEvict(cacheNames = {"localCache"}, key = "#root.methodName", cacheType = CacheType.LEVEL1CACHE)
    public void removeAllInfo() {
        log.info("删除缓存方法");
    }

    @U2Cacheable(cacheNames = {"localCache", "remoteCache"}, key = "#root.methodName", expireTime = 50, expireUnit = TimeUnit.SECONDS, cacheType = CacheType.LEVEL2CACHE)
    public Teacher findAllInfoLevel2() {
        log.info("进入二级缓存方法");
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
//        Map<String, String> map = new HashMap<>(100);
//        map.put("name", "zhou");
//        map.put("address", "hangzhou");
        return teacher;
    }

    @U2Cacheable(cacheNames = {"localCache"}, key = "#root.methodName", cacheType = CacheType.LEVEL1CACHE)
    public Map<String, Teacher> findAllMap() {
        log.info("进入缓存方法");
        Map<String, Teacher> map = new HashMap<>(16);
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
        map.put("aaa", teacher);
//        Map<String, String> map = new HashMap<>(100);
//        map.put("name", "zhou");
//        map.put("address", "hangzhou");
        return map;
    }

    @U2Cacheable(cacheNames = {"localCache", "remoteCache"}, key = "#root.methodName", cacheType = CacheType.LEVEL2CACHE)
    public Map<String, Teacher> findAllMapLevel2() {
        log.info("进入二级缓存方法");
        Map<String, Teacher> map = new HashMap<>(16);
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
        map.put("aaa", teacher);
//        Map<String, String> map = new HashMap<>(100);
//        map.put("name", "zhou");
//        map.put("address", "hangzhou");
        return map;
    }

    @U2Cacheable(cacheNames = {"localCache"}, key = "#root.methodName", cacheType = CacheType.LEVEL1CACHE)
    public List<Teacher> findLists() {
        log.info("进入缓存方法");
        List<Teacher> teachers = new ArrayList();
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
        teachers.add(teacher);
//        Map<String, String> map = new HashMap<>(100);
//        map.put("name", "zhou");
//        map.put("address", "hangzhou");
        return teachers;
    }

    @U2Cacheable(cacheNames = {"localCache", "remoteCache"}, key = "#root.methodName", cacheType = CacheType.LEVEL2CACHE)
    public List<Teacher> findListsLevel2() {
        log.info("进入二级缓存方法");
        List<Teacher> teachers = new ArrayList();
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
        teachers.add(teacher);
//        Map<String, String> map = new HashMap<>(100);
//        map.put("name", "zhou");
//        map.put("address", "hangzhou");
        return teachers;
    }

    //key = "#root.methodName+'['+#ttt.name+#ttt.age+']'"
    @U2Cacheable(cacheNames = {"localCache"}, key = "#root.methodName+'['+#ttt.name+']'", cacheType = CacheType.LEVEL1CACHE, expireTime = 1)
    public List<Teacher> findListsByInfo(Teacher ttt) {
        log.info("缓存失效，进入处理方法jji");
        List<Teacher> teachers = new ArrayList();
        for (int i = 1; i < 100; i++) {
            Teacher teacher = new Teacher();
            teacher.setName("lili");
            teacher.setAge(16);
            teachers.add(teacher);
        }
//        Map<String, String> map = new HashMap<>(100);
//        map.put("name", "zhou");
//        map.put("address", "hangzhou");
        return teachers;
    }

    @U2CacheEvict(cacheNames = {"localCache"}, key = "#root.methodName+'['+#hahaha+']'", cacheType = CacheType.LEVEL1CACHE)
    public List<Teacher> findListsByInfo(String hahaha) {
        log.info("删除缓存信息-----");
        return new ArrayList<Teacher>(100);
    }


    @U2Cacheable(cacheNames = {"localCache", "remoteCache"}, key = "'['+#ttt.name+#ttt.age+']'", cacheType = CacheType.LEVEL2CACHE, expireTime = 100, expireUnit = TimeUnit.SECONDS)
    public List<Teacher> findListsByInfoLevel2(Teacher ttt) {
        log.info("进入二级缓存方法");
        List<Teacher> teachers = new ArrayList();
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
        teachers.add(teacher);
        return teachers;
    }

    @U2CacheEvict(cacheNames = {"localCache", "remoteCache"}, key = "'['+#ttt.name+#ttt.age+']'", cacheType = CacheType.LEVEL2CACHE)
    public List<Teacher> removeListsByInfoLevel2(Teacher ttt) {
        log.info("删除二级缓存");
        List<Teacher> teachers = new ArrayList();
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
        teachers.add(teacher);
//        Map<String, String> map = new HashMap<>(100);
//        map.put("name", "zhou");
//        map.put("address", "hangzhou");
        return teachers;
    }

    @U2CachePut(cacheNames = {"localCache", "remoteCache"}, key = "'['+#ttt.name+#ttt.age+']'", cacheType = CacheType.LEVEL2CACHE)
    public List<Teacher> putListsByInfoLevel2(Teacher ttt) {
        log.info("删除二级缓存");
        List<Teacher> teachers = new ArrayList();
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
        teachers.add(teacher);
//        Map<String, String> map = new HashMap<>(100);
//        map.put("name", "zhou");
//        map.put("address", "hangzhou");
        return teachers;
    }


    /**
     * 事件发布处理
     *
     * @param t
     */
    @Autowired(required = false)
    private EventPush eventPush;

    public void pub(Teacher t) {
        eventPush.publish(new String[]{"tt", "remoteCache"}, "findListsByInfoLevel2[hahaha0]", EventType.clear, CacheType.LEVEL1CACHE);
    }

    @U2Cacheable(cacheNames = {"localCache"}, key = "'#root.methodName['+#ttt.name+#ttt.age+']'", cacheType = CacheType.LEVEL1CACHE, listenEventName = "eventTest")
    public List<Teacher> eventCacheLevel1Get(Teacher ttt) {
        log.info("获取一级缓存信息");
        List<Teacher> teachers = new ArrayList();
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
        teachers.add(teacher);
        return teachers;
    }

    @U2Cacheable(cacheNames = {"localCache1"}, key = "'#root.methodName['+#ttt.name+#ttt.age+']'", cacheType = CacheType.LEVEL1CACHE, listenEventName = "eventTest1")
    public List<Teacher> eventCacheLevel1Get1(Teacher ttt) {
        log.info("获取一级缓存信息");
        List<Teacher> teachers = new ArrayList();
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
        teachers.add(teacher);
        return teachers;
    }

    @U2CacheEvict(cacheType = CacheType.LEVEL1CACHE, listenEventName = {"eventTest1", "eventTest"})
    public List<Teacher> eventCacheLevel1remove(Teacher t) {
        log.info("删除一级缓存信息");
        List<Teacher> teachers = new ArrayList();
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
        teachers.add(teacher);
        return teachers;
    }

    @U2Cacheable(cacheNames = {"localCache"}, key = "'#root.methodName['+#ttt.name+#ttt.age+']'", cacheType = CacheType.LEVEL2CACHE, listenEventName = "eventTest")
    public List<Teacher> eventCacheLevel2Get(Teacher ttt) {
        log.info("获取二级缓存信息");
        List<Teacher> teachers = new ArrayList();
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
        teachers.add(teacher);
        return teachers;
    }

    @U2Cacheable(cacheNames = {"localCache1"}, key = "'#root.methodName['+#ttt.name+#ttt.age+']'", cacheType = CacheType.LEVEL2CACHE, listenEventName = "eventTest1")
    public List<Teacher> eventCacheLevel2Get1(Teacher ttt) {
        log.info("获取二级缓存信息");
        List<Teacher> teachers = new ArrayList();
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
        teachers.add(teacher);
        return teachers;
    }

    @U2CacheEvict(cacheType = CacheType.LEVEL2CACHE, listenEventName = {"eventTest1", "eventTest"})
    public List<Teacher> eventCacheLevel2remove(Teacher ttt) {
        log.info("删除二级缓存信息");
        List<Teacher> teachers = new ArrayList();
        Teacher teacher = new Teacher();
        teacher.setName("lili");
        teacher.setAge(16);
        teachers.add(teacher);
        return teachers;
    }



}
